package tech.abing.spider.task.pageprocessor.sac;

import java.util.List;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.sac.SecComEntity;
import tech.abing.spider.task.model.sac.SecSubComEntity;
import tech.abing.spider.task.pageprocessor.amac.FuturesAccountProcessor;
import tech.abing.spider.task.utils.TaskUtil;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class SecuritiesSubCompanyProcessor implements PageProcessor {

	private static final String TOP_URL = "http://jg.sac.net.cn/pages/publicity/indicatorsFrame.html";
	
	private static Logger logger = Logger.getLogger(SecuritiesSubCompanyProcessor.class);
	
	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		
		JSONObject jsonObj = JSONObject.fromObject(page.getContent());
		boolean hasPre = jsonObj.getBoolean("hasPre");
		String aoiId = (String)page.getRequest().getExtParams().get("aoiId");
		if(!hasPre){//第一页
			long totalPages = jsonObj.getLong("totalPages");
			
			PageRequest request = page.getRequest();
			
			if(totalPages > 1){
				for(int i = 2; i <= totalPages; i++){
					PageRequest req = context.createPageRequest(request.getUrl(), SecuritiesSubCompanyProcessor.class);
					req.setMethod(request.getMethod());
					req.setPostType(request.getPostType());
					
					req.setHeaders(request.getHeaders());
					
					req.putParams("filter_LIKES_mboi_branch_full_name", "");
					req.putParams("filter_LIKES_mboi_off_address", "");
					req.putParams("filter_EQS_aoi_id", aoiId);
					req.putParams("page.searchFileName", "publicity");
					req.putParams("page.sqlKey", "PAG_BRANCH_ORG");
					req.putParams("page.sqlCKey", "SIZE_BRANCH_ORG");
					req.putParams("_search", "false");
					req.putParams("nd",TaskUtil.getRandomStrByDate());
					req.putParams("page.pageSize", "15");
					req.putParams("page.pageNo", String.valueOf(i));
					req.putParams("page.orderBy", "MATO_UPDATE_DATE");
					req.putParams("page.order", "desc");
					
					req.getExtParams().put("aoiId", aoiId);
					
					queue.add(req);
				}
			}
		}
		
		//解析数据
		JSONArray result = jsonObj.getJSONArray("result");
		if(result != null && result.size() > 0){
			for(int i = 0; i < result.size(); i++){
				JSONObject retObj = result.getJSONObject(i);
				logger.info("正在解析url："+TOP_URL+"?aoi_id="+aoiId+"&is_org_search=no");
				SecSubComEntity ssc = new SecSubComEntity();
				ssc.setMboiBranchFullName(retObj.getString("MBOI_BRANCH_FULL_NAME"));
				ssc.setMboiBusinessScope(retObj.getString("MBOI_BUSINESS_SCOPE"));
				ssc.setMboiCsTel(retObj.getString("MBOI_CS_TEL"));
				ssc.setMboiOffAddress(retObj.getString("MBOI_OFF_ADDRESS"));
				ssc.setMboiPersonInCharge(retObj.getString("MBOI_PERSON_IN_CHARGE"));
				ssc.setRnum(retObj.getInt("RNUM"));
				ssc.setUrl(TOP_URL+"?aoi_id="+aoiId+"&is_org_search=no");
				ssc.setAoiId(aoiId);
				ssc.setId(UuIdPublic.timeStrongID(5));
				StringUtils.removeSpace(ssc);
				objectContainer.add(ssc);
			}
		}

	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub

	}

}
